//
//  SCOriginalView.swift
//  weibo
//
//  Created by 阿城 on 15/12/9.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit
import SnapKit

class SCOriginalView: UIView {

    var statusViewModel: SCStatusViewModel?{
        didSet{
            iconView.sd_setImageWithURL(NSURL(string: (statusViewModel?.statusMod?.user?.avatar_large)!), placeholderImage: UIImage(named: "avatar_default"))
            nameLabel.text = statusViewModel?.statusMod?.user?.name
            vipView.image = statusViewModel?.vipImage
            verifiedView.image = statusViewModel?.vertifiedImage
            sourceLabel.text = statusViewModel?.sourceText
            timeLabel.text = statusViewModel?.timeStr
            contentLabel.attributedText = statusViewModel?.originalAttributeStr
            contentLabel.linkResults = statusViewModel?.originalMatchResult
            
            picView.picsUrl = statusViewModel?.statusMod?.pic_urls
            
            //更换约束
            if picView.picsUrl?.count != 0 {
                picView.hidden = false
                viewBottomConstraint?.uninstall()
                self.snp_updateConstraints{ (make) -> Void in
                    viewBottomConstraint = make.bottom.equalTo(picView).offset(kMargin).constraint
                }
            }else{
                picView.hidden = true
                viewBottomConstraint?.uninstall()
                self.snp_updateConstraints{ (make) -> Void in
                    viewBottomConstraint = make.bottom.equalTo(contentLabel).offset(kMargin).constraint
                }
            }
        }
    }
    
    lazy var iconView = UIImageView(image: UIImage(named: "avatar_default"))
    lazy var nameLabel = UILabel(titleText: "未知用户", color: UIColor.darkGrayColor(), fontSize: 14)
    lazy var vipView = UIImageView(image: UIImage(named: "common_icon_membership"))
    lazy var timeLabel = UILabel(titleText: "刚--刚", color: UIColor.orangeColor(), fontSize: 10)
    lazy var sourceLabel = UILabel(titleText: "by XXXX", color: UIColor.lightGrayColor(), fontSize: 10)
    lazy var verifiedView = UIImageView(image: UIImage(named: "avatar_vip"))
    lazy var contentLabel: SCContentLabel = SCContentLabel(titleText: "", color: UIColor.darkGrayColor(), fontSize: kStatusFont)
    private lazy var picView: SCPictureView = SCPictureView()
    
    //记录约束
    private var viewBottomConstraint: Constraint?
    
    private func setupUI(){
        
        //添加控件
        iconView.cornerRadius = iconView.width * 0.5
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(vipView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(picView)
        contentLabel.numberOfLines = 0
        backgroundColor = UIColor.whiteColor()
        
        //各个约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(kMargin)
            make.top.equalTo(self).offset(kMargin)
            make.size.equalTo(CGSize(width: 42, height: 42))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(kMargin)
        }
        vipView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(nameLabel.snp_trailing).offset(kMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(kMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(timeLabel.snp_trailing).offset(kMargin)
        }
        let change: CGFloat = iconView.width * 0.5 * (1 - pow(2, 0.5) * 0.5)
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing).offset(-change)
            make.centerY.equalTo(iconView.snp_bottom).offset(-change)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.trailing.equalTo(self).offset(-kMargin)
            make.top.equalTo(iconView.snp_bottom).offset(kMargin)
        }
        picView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(kMargin)
            make.leading.equalTo(contentLabel)
        }
        self.snp_makeConstraints { (make) -> Void in
            viewBottomConstraint = make.bottom.equalTo(picView).offset(kMargin).constraint
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
